package org.tom.org.service.impl;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tom.basic.service.impl.BaseServiceImpl;
import org.tom.basic.util.MD5Utils;
import org.tom.basic.util.PageList;
import org.tom.basic.util.StrUtils;
import org.tom.org.domain.Employee;
import org.tom.org.mapper.EmployeeMapper;
import org.tom.org.query.EmployeeQuery;
import org.tom.org.service.IEmployeeService;
import org.tom.org.util.Employee2Logininfo;
import org.tom.user.domain.Logininfo;
import org.tom.user.mapper.LogininfoMapper;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void insert(Employee employee) {
        // 将填入的密码加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        // 修改对象中的salt盐值和password密码的值
        employee.setSalt(salt);
        employee.setPassword(password);
        // 调用方法将employee里面的信息填入logininfo中间
        Logininfo logininfo = Employee2Logininfo.emp2Logininfo(employee);
        // 在添加员工的同时将t_logininfo表中同时改变，并且返回自增的主键
        logininfoMapper.add(logininfo);
        // 将返回的自增主键填入到employee中间
        employee.setLogininfo(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.add(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        // 通过employee中idlogininfo_id来找到logininfo对象
        Long logininfo_id = employee.getLogininfo_id();

        // 调用方法将employee里面的信息填入logininfo中间
        Logininfo logininfo = Employee2Logininfo.emp2Logininfo(employee);
        // 将employee中的包含的logininfo对象中的id填到logininfo对象中
        logininfo.setId(employee.getLogininfo_id());
        // 在修改员工的同时将t_logininfo表中同时改变
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);
    }
}
